/*
    ajax请求函数模块
    返回值: promise对象(异步返回的数据是: response.data)
 */
import axios from 'axios'
import { Catch } from '.';

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.transformRequest = [
//     function(data){
//         const ret = new FormData();
//         for (const it in data) {
//             if(Array.isArray(data[it])){
//                 for (let i = 0; i < data[it].length; i++) {
//                     ret.append(it,data[it][i])
//                 }
//             }else{
//                 ret.append(it,data[it])
//             }
//         }
//         return ret;
//     }
// ];
/* eslint-disable */
export interface YProps{
	[key:string]:any
}
/* eslint-enable */
type MethodsProps = 'GET'|'POST'|'PUT';
interface HeaderProps{
	headers:{
		Authorization:string,
		[key:string]:any
	}
}
export const all = <T>(url:string, data:YProps, type:MethodsProps='GET'):Promise<T>=>{
	
	return new Promise(function (resolve, reject) {
	// 执行异步ajax请求
		let promise:Promise<T>
		if (type.toUpperCase() === 'GET') {
			// 准备url query参数数据
			let dataStr = '' //数据拼接字符串
			Object.keys(data).forEach(key => {
				dataStr += key + '=' + data[key] + '&'
			})
			if (dataStr !== '') {
				dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
				url = url + '?' + dataStr
			}
			// 发送get请求
			promise = axios.get(url)
		}else if(type.toUpperCase() === 'PUT'){
			promise = axios.put(url, data)
		}else {
			// 发送post请求
			promise = axios.post(url, data)
		}
		promise.then(function (response:any) {
			// 成功了调用resolve()
			resolve(response.data)
		}).catch(function (error) {
			reject(error)
		})
	})
}

export const auth =<T>(url:string, data:YProps, type:MethodsProps='GET',header:HeaderProps={
	headers:{
		'Authorization': Catch.getSession('access_token')||''
	}
}):Promise<T>=>{
	return new Promise(function (resolve, reject) {
	// 执行异步ajax请求
		let promise:Promise<T>
		if (type.toUpperCase() === 'GET') {
			// 准备url query参数数据
			let dataStr = '' //数据拼接字符串
			Object.keys(data).forEach(key => {
				dataStr += key + '=' + data[key] + '&'
			})
			if (dataStr !== '') {
				dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
				url = url + '?' + dataStr
			}
			// 发送get请求
			promise = axios.get(url, header)
		}else if(type.toUpperCase() === 'PUT'){
			promise = axios.put(url, data, header)
		} else {
			// 发送post请求
			promise = axios.post(url, data, header)
		}
		promise.then(function (response:any) {
			// 成功了调用resolve()
			resolve(response.data)
		}).catch(function (error) {
			reject(error)
		})
	})
}


export const upload =<T>(url:string, data:YProps, type:MethodsProps='POST',header:HeaderProps={
	headers:{
		'Authorization': Catch.getSession('access_token')||'',
		"Content-Type": "multipart/form-data"
	}
}):Promise<T>=>{
	return new Promise(function (resolve, reject) {
	// 执行异步ajax请求
		let promise:Promise<T>
		if (type.toUpperCase() === 'GET') {
			// 准备url query参数数据
			let dataStr = '' //数据拼接字符串
			Object.keys(data).forEach(key => {
				dataStr += key + '=' + data[key] + '&'
			})
			if (dataStr !== '') {
				dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
				url = url + '?' + dataStr
			}
			// 发送get请求
			promise = axios.get(url, header)
		}else if(type.toUpperCase() === 'PUT'){
			promise = axios.put(url, data, header)
		} else {
			promise = axios.post(url, data, header)
		}
		promise.then(function (response:any) {
			// 成功了调用resolve()
			resolve(response.data)
		}).catch(function (error) {
			reject(error)
		})
	})
}

export default {
	all,
	auth,
	upload
};